package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Resident;
import com.ruoyi.system.domain.vo.VisitorVo;
import com.ruoyi.system.service.IResidentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Visitor;
import com.ruoyi.system.service.IVisitorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 小区访客信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Controller
@RequestMapping("/system/visitor")
public class VisitorController extends BaseController
{
    private String prefix = "system/visitor";

    @Autowired
    private IVisitorService visitorService;

    @Resource
    private IResidentService residentService;

    @RequiresPermissions("system:visitor:view")
    @GetMapping()
    public String visitor()
    {
        return prefix + "/visitor";
    }

    /**
     * 查询小区访客信息列表
     */
    @RequiresPermissions("system:visitor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Visitor visitor)
    {
        startPage();
        visitor.setIsDelete(0);
        List<Visitor> list = visitorService.selectVisitorList(visitor);
        List<VisitorVo> visitorVos = new ArrayList<>();
        list.forEach(item -> {
            Long residentId = item.getResidentId();
            Resident resident = residentService.selectResidentById(residentId);
            VisitorVo visitorVo = new VisitorVo();
            BeanUtils.copyProperties(item, visitorVo);
            if (resident != null) {
                visitorVo.setResidentName(resident.getName());
            }
            visitorVos.add(visitorVo);
        });
        return getDataTable(visitorVos);
    }

    /**
     * 导出小区访客信息列表
     */
    @RequiresPermissions("system:visitor:export")
    @Log(title = "小区访客信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Visitor visitor)
    {
        List<Visitor> list = visitorService.selectVisitorList(visitor);
        ExcelUtil<Visitor> util = new ExcelUtil<Visitor>(Visitor.class);
        return util.exportExcel(list, "小区访客信息数据");
    }

    /**
     * 新增小区访客信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Resident> residents = residentService.selectResidentList(new Resident());
        mmap.put("residents", residents);
        return prefix + "/add";
    }

    /**
     * 新增保存小区访客信息
     */
    @RequiresPermissions("system:visitor:add")
    @Log(title = "小区访客信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Visitor visitor)
    {
        visitor.setCreateTime(new Date());
        visitor.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        visitor.setCreateBy(String.valueOf(userId));
        visitor.setUpdateBy(String.valueOf(userId));
        return toAjax(visitorService.insertVisitor(visitor));
    }

    /**
     * 修改小区访客信息
     */
    @RequiresPermissions("system:visitor:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Visitor visitor = visitorService.selectVisitorById(id);
        mmap.put("visitor", visitor);
        List<Resident> residents = residentService.selectResidentList(new Resident());
        mmap.put("residents", residents);
        return prefix + "/edit";
    }

    /**
     * 修改保存小区访客信息
     */
    @RequiresPermissions("system:visitor:edit")
    @Log(title = "小区访客信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Visitor visitor)
    {
        visitor.setUpdateTime(new Date());
        Long userId = ShiroUtils.getUserId();
        visitor.setUpdateBy(String.valueOf(userId));
        return toAjax(visitorService.updateVisitor(visitor));
    }

    /**
     * 删除小区访客信息
     */
    @RequiresPermissions("system:visitor:remove")
    @Log(title = "小区访客信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(visitorService.softDeleteVisitorByIds(ids));
    }
}
